import { Component, Injector, ViewEncapsulation, ViewChild, ElementRef } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Http } from '@angular/http';
import { MaintenanceServiceProxy, MaintenanceListDto } from '@shared/service-proxies/service-proxies';
import { NotifyService } from '@abp/notify/notify.service';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { TokenAuthServiceProxy } from '@shared/service-proxies/service-proxies';
import { CreateOrEditMaintenanceModalComponent } from './create-or-edit-maintenance-modal.component';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { DataTable } from 'primeng/components/datatable/datatable';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';

import * as moment from 'moment';
import { Table } from 'primeng/table';
import { BoundEquipmentMaintenancesModalComponent } from './bound-equipment-maintenances-modal.component';

@Component({
    templateUrl: './maintenance.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})

export class MaintenancesComponent extends AppComponentBase {
    @ViewChild('DatePicker') datePicker: ElementRef;
    @ViewChild('createOrEditMaintenanceModal') createOrEditMaintenanceModal: CreateOrEditMaintenanceModalComponent;
    @ViewChild('boundEquipmentMaintenancesModal') boundEquipmentMaintenancesModal: BoundEquipmentMaintenancesModalComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    //Filters
    es: any;
    advancedFiltersAreShown = false;
    filterText = '';
    selectedPermission = '';
    rangeDates1: Date[];
    rangeDates2: Date[];
    rangeDates3: Date[];
    startDateBegin: moment.Moment = undefined;
    startDateEnd: moment.Moment = undefined;
    finishDateBegin: moment.Moment = undefined;
    finishDateEnd: moment.Moment = undefined;
    creatDateBegin: moment.Moment = undefined;
    creatDateEnd: moment.Moment = undefined;

    constructor(
        injector: Injector,
        private _http: Http,
        private _maintenanceServiceProxy: MaintenanceServiceProxy,
        private _notifyService: NotifyService,
        private _fileDownloadService: FileDownloadService,
        private _tokenAuth: TokenAuthServiceProxy,
        private _activatedRoute: ActivatedRoute
    ) {
        super(injector);
        this.filterText = this._activatedRoute.snapshot.queryParams['filterText'] || '';
    }

    getMaintenances(event?: LazyLoadEvent) {
        this.primengTableHelper.showLoadingIndicator();

        this.startDateBegin = undefined,
            this.startDateEnd = undefined,
            this.finishDateBegin = undefined,
            this.finishDateEnd = undefined,
            this.creatDateBegin = undefined,
            this.creatDateEnd = undefined,

            this._maintenanceServiceProxy.getEquipMaintenances(
                this.filterText,
                this.startDateBegin,
                this.startDateEnd,
                this.finishDateBegin,
                this.finishDateEnd,
                this.creatDateBegin,
                this.creatDateEnd,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            ).subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;


                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    getEquipMaintenances1(event?: LazyLoadEvent) {

        this.primengTableHelper.showLoadingIndicator();

        if (this.rangeDates1 != null) {
            this.startDateBegin = moment(this.rangeDates1[0]),
                this.startDateEnd = moment(this.rangeDates1[1])
        }
        if (this.rangeDates2 != null) {
            this.finishDateBegin = moment(this.rangeDates2[0]),
                this.finishDateEnd = moment(this.rangeDates2[1])
        }
        if (this.rangeDates3 != null) {
            this.creatDateBegin = moment(this.rangeDates3[0]),
                this.creatDateEnd = moment(this.rangeDates3[1])
        }

        this._maintenanceServiceProxy.getEquipMaintenances(
            this.filterText,
            this.startDateBegin,
            this.startDateEnd,
            this.finishDateBegin,
            this.finishDateEnd,
            this.creatDateBegin,
            this.creatDateEnd,
            // moment(this.rangeDates1[0]),
            // moment(this.rangeDates1[1]),
            // moment(this.rangeDates2[0]),
            // moment(this.rangeDates2[1]),
            // moment(this.rangeDates2[0]),
            // moment(this.rangeDates2[1]),
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();

        });

    }

    reloadPage(): void {
        this.paginator.changePage(this.paginator.getPage());
    }

    exportToExcel(): void {
    }


    deleteMaintenance(maintenance: MaintenanceListDto): void {
        this.message.confirm(
            '确认删除该假期检修吗',
            (isConfirmed) => {
                if (isConfirmed) {
                    this._maintenanceServiceProxy.deleteEquipMaintenance(maintenance.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }
}
